package org.grow.stable.security;

import org.grow.stable.security.dao.UserDao;
import org.grow.stable.security.domain.User;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


@Service
public class ColdDaoAuthProvider extends DaoAuthenticationProvider implements UserDetailsService {
    @Resource
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User target = userDao.findByUsername(s);
        //这里可以来一个钩子函数 意义是格式化:将数据库查询出来的内容转化成为UserDetail接口的内容
        System.out.println(target);
        return target;
    }



    @Resource
    PasswordEncoder passwordEncoder;
    @PostConstruct
    public void init(){

        this.setUserDetailsService(this);
        this.setPasswordEncoder(passwordEncoder);
    }
}
